package com.example.bot_cc.zyfypt_420clj.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.bot_cc.zyfypt_420clj.R;
import com.example.bot_cc.zyfypt_420clj.bean.LoginBean;
import com.example.bot_cc.zyfypt_420clj.iface.LoginListener;
import com.example.bot_cc.zyfypt_420clj.model.LoginModel;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private Button btn,btn1;
    private EditText idET,pwdEt;
    private Switch sw;

    private String username="",password="",sessionid="";//用户名密码sessionid
    private SharedPreferences sp;//SharedPreferences是Android平台上一个轻量级的存储类，用来保存应用的一些常用配置

    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            System.out.println("result:"+loginBean.getId());
            if(loginBean.getId()!=null) {
                sessionid=loginBean.getSessionid().toString();
                saveSP();
                Intent intent1 = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent1);
            }
        }

        @Override
        public void onFail(String msg) {
            System.out.println("fail");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Toast.makeText(this, "登录", Toast.LENGTH_SHORT).show();
        init();

        //实例化sp
        sp=getSharedPreferences("login",MODE_PRIVATE);
        //读取sp
        readsp();
    }

    //读取sp
    private void readsp() {
        String name=sp.getString("username",null);
        String pass=sp.getString("password",null);
        String sessionid=sp.getString("sessionid",null);
        Boolean b=sp.getBoolean("remember",false);
        if(b) {
            idET.setText(name);
            pwdEt.setText(pass);
            sw.setChecked(b);
        }
    }

    private void init() {
        btn=(Button)findViewById(R.id.button);
        btn1=(Button)findViewById(R.id.button2);
        idET = (EditText)findViewById(R.id.editText);
        pwdEt = (EditText)findViewById(R.id.editText2);
        sw=(Switch) findViewById(R.id.switch1);


        btn.setOnClickListener(this);
        btn1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                System.out.println("点击登录按钮！");
                LoginModel loginModel = new LoginModel();
                loginModel.getResult(username=idET.getText().toString(),
                                     password=pwdEt.getText().toString(),
                                     loginListener);
                saveSP();
                break;
            case R.id.button2:
                System.out.println("点击注册按钮！");
                Intent intent = new Intent();
                intent.setClass(this, RegisterActivity.class);
                startActivity(intent);
                break;
        }
    }

    //保存sharedpreferences
    private void saveSP() {
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putString("sessionid",sessionid);
        editor.putBoolean("remember",sw.isChecked());
        editor.commit();//提交保存
    }
}
